package com.pospi.pospihold.fragment;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.pospi.pospihold.R;
import com.pospi.pospihold.adapter.HomeAdapter;
import com.pospi.pospihold.dto.TitleBeen;
import com.pospi.pospihold.ui.MainActivity;
import com.pospi.pospihold.ui.cash_ui.DailyActivity;
import com.pospi.pospihold.ui.cash_ui.FastCheckActivity;
import com.pospi.pospihold.ui.cash_ui.OpenShopActivity;
import com.pospi.pospihold.ui.cash_ui.PointSelectActivity;
import com.pospi.pospihold.ui.cash_ui.RefundActivity;
import com.pospi.pospihold.ui.cash_ui.RestOrderActivity;
import com.pospi.pospihold.ui.cash_ui.ScanReceiptActivity;
import com.pospi.pospihold.ui.login_ui.CashierLoginActivity;
import com.pospi.pospihold.util.RecycleViewDivider;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CashierFragment extends BaseFragment {
    @Bind(R.id.ll_scan_receipt)
    LinearLayout llScanReceipt;
    @Bind(R.id.ll_poi_receipt)
    LinearLayout llPoiReceipt;
    @Bind(R.id.ll_quick_receipt)
    LinearLayout llQuickReceipt;
    @Bind(R.id.ll_refund)
    LinearLayout llRefund;
    @Bind(R.id.ll_rest_order)
    LinearLayout llRestOrder;
    @Bind(R.id.ll_shift_change)
    LinearLayout llShiftChange;
    @Bind(R.id.ll_open_shop)
    LinearLayout llOpenShop;
    @Bind(R.id.ll_daily)
    LinearLayout llDaily;
    @Bind(R.id.iv_menu)
    ImageView ivMenu;
    @Bind(R.id.ll_cahier)
    FrameLayout llCahier;
    @Bind(R.id.rv_main)
    RecyclerView recyclerView;

    private Context context;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_cashier, container, false);
        ButterKnife.bind(this, view);
        context = getActivity().getApplication();
        final List<TitleBeen> titleBeens = new ArrayList<>();
//        titleBeens.add(new TitleBeen("扫码收款",R.drawable.icon_sm));
        titleBeens.add(new TitleBeen("点单",R.drawable.icon_dxsk));

//        titleBeens.add(new TitleBeen("退款",R.drawable.icon_tk));
        titleBeens.add(new TitleBeen("取单",R.drawable.icon_gd));
        titleBeens.add(new TitleBeen("快速收款",R.drawable.icon_ks));
        titleBeens.add(new TitleBeen("订单列表",R.drawable.icon_rj));
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        HomeAdapter homeAdapter = new HomeAdapter(R.layout.item_title, titleBeens);
        recyclerView.setAdapter(homeAdapter);
        homeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                switch (titleBeens.get(position).getName()) {
//                    case "扫码收款":
//                        startActivity(ScanReceiptActivity.class);
//                        break;
                    case "点单":
                        startActivity(PointSelectActivity.class);
                        break;
                    case "快速收款":
                        startActivity(FastCheckActivity.class);
                        break;
//                    case "退款":
//                        startActivity(RefundActivity.class);
//                        break;
                    case "取单":
                        startActivity(RestOrderActivity.class);
                        break;
                    case "订单列表":
                        startActivity(RefundActivity.class);
                        break;
                }
            }
        });
//        recyclerView.addItemDecoration(new RecycleViewDivider(getContext(), LinearLayoutManager.HORIZONTAL));
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @TargetApi(Build.VERSION_CODES.KITKAT)
    @OnClick({R.id.iv_menu, R.id.ll_scan_receipt, R.id.ll_poi_receipt, R.id.ll_quick_receipt, R.id.ll_refund, R.id.ll_rest_order, R.id.ll_shift_change, R.id.ll_open_shop, R.id.ll_daily})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_menu:

                MainActivity mainActivity = (MainActivity) getActivity();
                mainActivity.openLeftDrawer();

//                LayoutInflater mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//                View contentView = mLayoutInflater.inflate(R.layout.main_pop_left, null);
//                // R.layout.pop为PopupWindow 的布局文件
//                PopupWindow pop = new PopupWindow(contentView, TurnSize.dip2px(context, 250), TurnSize.dip2px(context, 100));
//                pop.setBackgroundDrawable(new BitmapDrawable());
//                // 指定 PopupWindow 的背景
//                pop.setFocusable(true);
//                pop.showAsDropDown(llCahier);
//                TextView storeNo = (TextView) contentView.findViewById(R.id.store_no);
//                TextView nameIdTime = (TextView) contentView.findViewById(R.id.name_id_time);
//
//                String storeName = context.getSharedPreferences("StoreMessage", Context.MODE_PRIVATE).getString("Name", "");
//                String value = context.getSharedPreferences("Token", Context.MODE_PRIVATE).getString("value", "");
//                String[] names = value.split("\\,");
//                String deviceNo = names[2];//得到token的值
//                int which = context.getSharedPreferences("islogin", Context.MODE_PRIVATE).getInt("which", 0);
//                List<CashierMsgDto> cashierMsgDtos = new PareseJson().parese(context.getSharedPreferences("cashierMsgDtos", Context.MODE_PRIVATE).getString("cashierMsgDtos", ""));
//                String name = cashierMsgDtos.get(which).getName();
//                String id = cashierMsgDtos.get(which).getNumber();
//                storeNo.setText(storeName + "-" + deviceNo);
//                nameIdTime.setText(name + "-" + id + "-" + GetData.getYYMMDDshow());

                break;
            case R.id.ll_scan_receipt:
                startActivity(ScanReceiptActivity.class);
                break;
            case R.id.ll_poi_receipt:
                startActivity(PointSelectActivity.class);
                break;
            case R.id.ll_quick_receipt:
                startActivity(FastCheckActivity.class);
                break;
            case R.id.ll_refund:
                startActivity(RefundActivity.class);
                break;
            case R.id.ll_rest_order:
                startActivity(RestOrderActivity.class);
                break;
            case R.id.ll_shift_change:
                startActivity(CashierLoginActivity.class);
                getActivity().finish();
                break;
            case R.id.ll_open_shop:
                startActivity(OpenShopActivity.class);
                break;
            case R.id.ll_daily:
                startActivity(DailyActivity.class);
                break;
        }
    }
}
